package com.qf.air.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author 10537
 * @version V1.0
 * @Date 2021/11/1 20:15
 */
@Component
public class RedisUtils {



    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置key-value，并制定key的生存时间
     * @param key
     * @param secong
     * @param value
     */
    public void set(String key,int secong,String value){
        stringRedisTemplate.opsForValue().set(key,value,secong, TimeUnit.SECONDS);
    }

    /**
     * 根据key获取value
     * @param key
     * @return
     */
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 重新设置key的生存时间
     * @param key
     * @param second
     */
    public void expire(String key,int second){
        stringRedisTemplate.expire(key,second,TimeUnit.SECONDS);
    }
}
